<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 600px;
            height: 400px;
            border: 5px solid green;
            border-collapse: collapse;
        }

        table tr th,
        table tr td {
            border: 1px solid blue;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <th>序号</th>
                <th>名称</th>
                <th>价格</th>
                <th>数量</th>
                <th>小计</th>
            </tr>
            <tr v-for="(item,index) in shop" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="item.num--" :disabled="item.num <=1">-</button>
                    {{item.num}}
                    <button @click="item.num++">+</button>
                    <button @click="del(index)">x</button>
                </td>
                <td>{{item.price * item.num}}</td>
            </tr>
        </table>
        <div>总计:{{total}} | 总价格:{{totalPrice}} </div>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                shop: [
                    { "id": 1, "name": "小米", "price": 1000, "num": 1 },
                    { "id": 2, "name": "华为", "price": 2000, "num": 1 },
                    { "id": 3, "name": "oppo", "price": 3000, "num": 1 },
                    { "id": 4, "name": "vivo", "price": 4000, "num": 1 },
                ]
            },
            methods: {
                del(index) {
                    confirm("确定要删除该条的信息吗") && this.shop.splice(index, 1);
                }
            },
            computed: {
                total() {
                    var t = 0;
                    this.shop.forEach(item => {
                        t += item.num;
                    })
                    return t;
                },
                totalPrice() {
                    var t = 0;
                    this.shop.forEach(item => {
                        t += item.price * item.num;
                    })
                    return t;
                }
            }
        })
    </script>
</body>

</html>